package com.toune.movie.base.myUtil;

import android.content.Context;
import android.util.AttributeSet;

import com.tencent.smtt.export.external.interfaces.SslError;
import com.tencent.smtt.export.external.interfaces.SslErrorHandler;
import com.tencent.smtt.export.external.interfaces.WebResourceResponse;
import com.tencent.smtt.sdk.WebChromeClient;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

public class ProgressWebview extends WebView {

    private NumberProgressBar progressbar;  //进度条

    private int progressHeight = 10;  //进度条的高度，默认10px


    public ProgressWebview(Context context) {
        super(context);
        initView(context);
    }

    public ProgressWebview(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        initView(context);

    }

    private void initView(Context context) {

        //开启js脚本支持
        getSettings().setJavaScriptEnabled(true);

        //创建进度条
//        progressbar = new ProgressBar(context, null,
//                android.R.attr.progressBarStyleHorizontal);
//        //设置加载进度条的高度
//        progressbar.setLayoutParams(new AbsoluteLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, progressHeight, 0, 0));
//
//        Drawable drawable = context.getResources().getDrawable(R.drawable.progress_bar_states);
//        progressbar.setProgressDrawable(drawable);

        progressbar = new NumberProgressBar(context, null);
        //添加进度到WebView
        addView(progressbar);

        //适配手机大小
        getSettings().setUseWideViewPort(true);
        getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        getSettings().setLoadWithOverviewMode(true);
        getSettings().setSupportZoom(true);
        getSettings().setBuiltInZoomControls(true);
        getSettings().setDisplayZoomControls(false);
        getSettings().setBlockNetworkImage(false);
        getSettings().setDatabaseEnabled(true);
        getSettings().setAppCacheEnabled(true);
        getSettings().setDomStorageEnabled(true);
//        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
//            getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
//        }
        getSettings().setUserAgentString(getSettings().getUserAgentString());
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
        getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);


        //cache setting
        getSettings().setAppCacheEnabled(true);
        getSettings().setAppCacheMaxSize(Long.MAX_VALUE);//设置缓存大小
        getSettings().setAppCachePath(MyUtil.getAppFilePath());//设置缓存目录
        getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        setWebChromeClient(new WVChromeClient());
        setWebViewClient(new WVClient());
    }


    //进度显示
    private class WVChromeClient extends WebChromeClient {

        @Override
        public void onProgressChanged(WebView view, int newProgress) {


            if (newProgress == 100) {
                progressbar.setVisibility(GONE);
            } else {
                if (progressbar.getVisibility() == GONE)
                    progressbar.setVisibility(VISIBLE);
                progressbar.setProgress(newProgress);
            }

            if (mListener != null) {
                mListener.onProgressChange(view, newProgress);
            }

            super.onProgressChanged(view, newProgress);
        }

    }

    private class WVClient extends WebViewClient {

        @Override
        public WebResourceResponse shouldInterceptRequest(WebView webView, String url) {
            url = url.toLowerCase();
            if (!ADFilterTool.hasAd(getContext(), url)) {
                return super.shouldInterceptRequest(webView, url);//正常加载
            } else {
                return new WebResourceResponse(null, null, null);//含有广告资源屏蔽请求
            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            if (url.contains("http") || url.contains("https")) {
                // 返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }
            return true;
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            //https忽略证书问题
            handler.proceed();
        }

        @Override
        public void onPageFinished(WebView view, String url) {

            progressbar.setVisibility(GONE);
            if (mListener != null) {
                mListener.onPageFinish(view);
            }

            super.onPageFinished(view, url);

        }

    }

    private onWebViewListener mListener;

    public void setOnWebViewListener(onWebViewListener listener) {
        this.mListener = listener;
    }

    //进度回调接口
    public interface onWebViewListener {
        void onProgressChange(WebView view, int newProgress);

        void onPageFinish(WebView view);
    }

}